package cn.lj.service.impl;

import cn.lj.common.handler.BusinessException;
import cn.lj.common.response.ResultCode;
import cn.lj.common.util.BigDecimalUtil;
import cn.lj.common.util.DataUtil;
import cn.lj.entity.ComparedVo;
import cn.lj.entity.FinanceInfo;
import cn.lj.entity.HistoryVo;
import cn.lj.mapper.FinanceInfoMapper;
import cn.lj.service.FinanceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李津
 * @since 2021-04-21
 */
@Service
public class FinanceInfoServiceImpl extends ServiceImpl<FinanceInfoMapper, FinanceInfo> implements FinanceInfoService {

    /**
     * 统一处理余额，支出-，收入+，一次修改所有余额为相同值
     * @param financeInfo
     */
    private String updateAllBalance(FinanceInfo financeInfo){
        //剩余金额自动完成加减
        String balance = findBalance();
        String sum = financeInfo.getSum();

        //String类型转换成double类型
        double b = Double.parseDouble(balance);
        double s = Double.parseDouble(sum);

        //统一余额值
        String value = "";

        //支出后的经费
        if (financeInfo.getType()==1){
            BigDecimal pay = BigDecimalUtil.sub(b, s);
            //经费如果不足支出,将抛出经费不足
            if (Double.parseDouble(String.valueOf(pay))<0){
                throw new BusinessException(ResultCode.FinanceInfo_NOT_SHORT.getCode(),ResultCode.FinanceInfo_NOT_SHORT.getMessage());
            }

            //double类型转换成String类型
            value = String.valueOf(pay);

            //收入后的经费
        }else if (financeInfo.getType()==2){
            BigDecimal income = BigDecimalUtil.add(b, s);
            //double类型转换成String类型
            value = String.valueOf(income);
        }

        this.baseMapper.updateAllBalance(value);
        return value;
    }

    /**
     * 查询任意的列单一的balance
     * @return
     */
    private String findBalance(){
        return this.baseMapper.findBalance();
    }

    @Override
    public void addFinanceInfo(FinanceInfo financeInfo) {
        //设置逻辑删除为未删除
        financeInfo.setIsDeleted(0);

        String value = updateAllBalance(financeInfo);
        financeInfo.setBalance(value);

        //使用mybatis-plus的自动设置创建时间和更新时间
        this.baseMapper.insert(financeInfo);
    }

    @Override
    public void updateFinanceInfo(FinanceInfo financeInfo) {
        String value = updateAllBalance(financeInfo);
        financeInfo.setBalance(value);

        //使用mybatis-plus的自动设置创建时间和更新时间
        this.baseMapper.updateById(financeInfo);
    }

    /**
     * 分页查询用户
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<FinanceInfo> findFinanceInfoPage(Page<FinanceInfo> page, QueryWrapper<FinanceInfo> queryWrapper) {
        return this.baseMapper.findFinanceInfoPage(page,queryWrapper);
    }

    /**
     * 对比饼图
     * @return
     */
    @Override
    public List<ComparedVo> ComparedVoList() {
        List<ComparedVo> comparedVos = new ArrayList<>();
        List<FinanceInfo> financeInfos = this.baseMapper.selectList(null);

        for (FinanceInfo financeInfo : financeInfos) {
            ComparedVo comparedVo = new ComparedVo();
            Integer sum = Integer.valueOf(financeInfo.getSum());
            comparedVo.setValue(sum);
            comparedVo.setName(financeInfo.getReason());
            comparedVo.setItemStyle(DataUtil.createItemStyle(sum));
            comparedVos.add(comparedVo);
        }
        return comparedVos;
    }

    /**
     * 历史折线图
     * @return
     */
    @Override
    public List<HistoryVo> getHistories() {
        List<HistoryVo> historyVos = this.baseMapper.getHistories();
/*        HashMap<String, Object> res = new HashMap<>();
        List<String> balances = new ArrayList<>();
        List<Date> reasonTimes = new ArrayList<>();

        for (HistoryVo historyVo : historyVos) {
            balances.add(historyVo.getBalance());
            reasonTimes.add(historyVo.getReasonTime());
        }

        res.put("balances",balances);
        res.put("reasonTimes",reasonTimes);*/

        return historyVos;
    }
}
